package com.example.linyiquan.application.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.example.linyiquan.application.R;
import com.example.linyiquan.application.util.MyVolley;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

import static android.R.attr.name;

/**
 * Created by linyiquan on 17-4-20.
 */

public class LoginActivity extends Activity implements View.OnClickListener {

    private EditText et_username;
    private EditText et_password;
    private TextView tv_register;
    private CheckBox cb_password;
    private Button bt_login;

    private String username;
    private String password;
    private SharedPreferences sp;
    private SharedPreferences.Editor spe;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        sp = getSharedPreferences("check", MODE_PRIVATE);
        initView();
    }

    private void initView() {
        et_username = (EditText) findViewById(R.id.et_username);
        et_username.setText(sp.getString("username", username));
        et_password = (EditText) findViewById(R.id.et_password);
        tv_register = (TextView) findViewById(R.id.tv_register);
        tv_register.setOnClickListener(this);
        cb_password = (CheckBox) findViewById(R.id.cb_password);
        if (sp.getBoolean("check", false)) {
            et_username.setText(sp.getString("username", username));
            et_password.setText(sp.getString("password", password));
            cb_password.setChecked(true);
        }
        bt_login = (Button) findViewById(R.id.bt_login);
        bt_login.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_login:
                username = et_username.getText().toString();
                password = et_password.getText().toString();

                if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
                    Toast.makeText(this, "用户名密码要填写", Toast.LENGTH_SHORT).show();
                    return;
                }

                boolean checkbox = cb_password.isChecked();

                if (checkbox) {
                    spe = sp.edit();
                    spe.putString("username", username);
                    spe.putString("password", password);
                    spe.putBoolean("check", true);
                    spe.commit();
                } else {
                    spe = sp.edit();
                    spe.putString("username", username);
                    spe.putString("password", password);
                    spe.putBoolean("check", false);
                    spe.commit();
                }

//                new Thread(new Runnable() {
//                    @Override
//                    public void run() {
//                        try {
//                            StringBuilder buf = new StringBuilder("http://120.76.96.34/weizhanjsp/user/login?clientType=1&mac=sdfsss&cards=true");
//                            buf.append("&username=" + URLEncoder.encode(username, "UTF-8"));
//                            buf.append("&password=" + URLEncoder.encode(password, "UTF-8"));
//                            URL url = new URL(buf.toString());
//                            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//                            conn.setRequestMethod("GET");
//                            if (conn.getResponseCode() == 200) {
//                                runOnUiThread(new Runnable() {
//                                    @Override
//                                    public void run() {
//                                        Toast.makeText(LoginActivity.this, "GET提交成功", Toast.LENGTH_SHORT).show();
//                                    }
//                                });
//                                Intent intent = new Intent(LoginActivity.this, GridActivity.class);
//                                startActivity(intent);
//                            } else {
//                                runOnUiThread(new Runnable() {
//                                    @Override
//                                    public void run() {
//                                        Toast.makeText(LoginActivity.this, "GET提交失败", Toast.LENGTH_SHORT).show();
//                                    }
//                                });
//                            }
//                        } catch (Exception e) {
//                            e.printStackTrace();
//                        }
//                    }
//                }).start();


//              POST请求
//              new Thread(new Runnable() {
//                    @Override
//                    public void run() {
//                        try {
//                            StringBuilder buf=new StringBuilder();
//                            buf.append("&username="+URLEncoder.encode(username,"Utf-8"));
//                            buf.append("&password="+URLEncoder.encode(password,"UTF-8"));
//                            byte[] data=buf.toString().getBytes("UTF-8");
//                            URL url=new URL("http://120.76.96.34/weizhanjsp/user/login?clientType=1&mac=sdfsss&cards=true");
//                            HttpURLConnection conn= (HttpURLConnection) url.openConnection();
//                            conn.setRequestMethod("POST");
//                            conn.setDoOutput(true);
//                            OutputStream out=conn.getOutputStream();
//                            out.write(data);
//                            if (conn.getResponseCode()==200){
//                                runOnUiThread(new Runnable() {
//                                    @Override
//                                    public void run() {
//                                        Toast.makeText(LoginActivity.this, "POST提交成功", Toast.LENGTH_SHORT).show();
//                                    }
//                                });
//                                Intent intent=new Intent(LoginActivity.this,GridActivity.class);
//                                startActivity(intent);
//                            }else {
//                                runOnUiThread(new Runnable() {
//                                    @Override
//                                    public void run() {
//                                        Toast.makeText(LoginActivity.this, "POST提交失败", Toast.LENGTH_SHORT).show();
//                                    }
//                                });
//                            }
//                        }catch (Exception e){
//                            e.printStackTrace();
//                        }
//                    }
//                }).start();;
                LogintoServer(username, password);

                break;

            case R.id.tv_register:

                Intent register = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(register);
                finish();
                break;
        }
    }

    private void LogintoServer(String username, String password) {
        MyVolley.init(this);
        String url = "http://120.76.96.34/weizhanjsp/user/login?clientType=1&mac=sdfsss&cards=true";

        url += "&username=" + username;
        url += "&password=" + password;
        MyVolley.post(url, null, new MyVolley.OnResultLitener() {
            @Override
            public void done(JSONObject result) {
                int resultCode;
                try {
                    resultCode = result.getInt("resultCode");
                    if (resultCode == 0) {
                        Toast.makeText(LoginActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(LoginActivity.this, GridActivity.class);
                        startActivity(intent);
                    } else {
                        Toast.makeText(LoginActivity.this, "用户名密码出错", Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void fail(VolleyError error) {
                Toast.makeText(LoginActivity.this, error.toString(), Toast.LENGTH_SHORT).show();
            }
        });
    }

}
